Skip to content

feat(os): seed os.clock origin at install, add os.time_ms/os.time_us - #340

Merged
davydog187 merged 2 commits into
mainfrom
feat/os-time-precision
Jun 9, 2026
Merged

davydog187 merged 2 commits into
mainfrom
feat/os-time-precision

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

Closes #330.

Context

Issue #330 flagged two problems with the os standard library:

  1. os.clock() origin was seeded lazily. The boot_offset/0 helper's comment claimed the monotonic origin was "captured at module load," but it was actually seeded on the first call to os.clock() — so the origin drifted to whenever a program first happened to call it.
  2. No sub-second epoch precision. os.time() returns whole seconds (correct PUC-Lua behavior), leaving programs that need finer resolution with no option.

Changes

  • Seed boot_offset() in install/1 so os.clock() measures elapsed time from a stable startup point. Updated the stale boot_offset/0 comment to describe the real (:persistent_term, BEAM-global, first-installer-wins) semantics.
  • Add os.time_ms() and os.time_us() — non-standard extensions returning the current epoch in milliseconds / microseconds via System.os_time/1. Both are current-time-only (no date-table arg, since sub-second precision on a whole-second date table is meaningless). os.time() is unchanged.
  • @moduledoc lists the new functions and explicitly marks them as extensions not present in PUC-Lua.

Tests

Added coverage in test/lua/vm/stdlib/os_test.exs for integer type and magnitude of both new functions, plus a cross-check that ms/us track os.time(). All os tests pass (mix test test/lua/vm/stdlib/os_test.exs → 12 passed).

Out of scope

  • Making os.clock() per-VM rather than BEAM-global (issue only asks to seed at install).
  • Date-table support for the _ms/_us variants.
  • Changing os.time()'s whole-second return.

Seed boot_offset() during install/1 so os.clock() measures elapsed time
from a stable startup point rather than from its first call. Add two
non-standard helpers, os.time_ms() and os.time_us(), returning the
current epoch in milliseconds and microseconds; os.time() keeps its
whole-second PUC-Lua semantics.

Closes #330
Comment thread lib/lua/vm/stdlib/os.ex Resolved
@davydog187
davydog187 merged commit 49ce267 into main Jun 9, 2026
5 checks passed
@davydog187
davydog187 deleted the feat/os-time-precision branch June 9, 2026 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Address issues with os.time

1 participant